﻿using System.IO;
using System.Runtime.Serialization;
using System.Xml.Serialization;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using Should;

namespace MugenMvvmToolkitTest.Infrastructure
{
    public abstract class SerializationTestBase<T>
    {
        [TestMethod]
        public virtual void TestXmlSerialization()
        {
            object obj = GetObject();
            using (var memStream = new MemoryStream())
            {
                var serializer = new XmlSerializer(obj.GetType());
                serializer.Serialize(memStream, obj);
                memStream.Position = 0;
                var result = serializer.Deserialize(memStream);
                result.ShouldBeType(obj.GetType());
                AssertObject((T)result);
            }
        }

        [TestMethod]
        public virtual void TestDataContractSerialization()
        {
            object obj = GetObject();
            using (var memStream = new MemoryStream())
            {
                var serializer = new DataContractSerializer(obj.GetType());
                serializer.WriteObject(memStream, obj);
                memStream.Position = 0;
                var result = serializer.ReadObject(memStream);
                result.ShouldBeType(obj.GetType());
                AssertObject((T)result);
            }
        }

#if WPF
        [TestMethod]
        public virtual void TestBinarySerialization()
        {
            object obj = GetObject();
            using (var memStream = new MemoryStream())
            {
                var serializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                serializer.Serialize(memStream, obj);
                memStream.Position = 0;
                var result = serializer.Deserialize(memStream);
                result.ShouldBeType(obj.GetType());
                AssertObject((T) result);
            }
        }
#endif
        public abstract T GetObject();

        public abstract void AssertObject(T deserializedObj);
    }
}